package main

import (
	"fmt"
	"net/http"
	"encoding/json"
	"io/ioutil"
	"strings"
)

type AnankeServer struct {

}

type JournalEntry struct {
	Book uint16 `json:"book"`
	Page uint16 `json:"page"`
	Title string `json:"title"`
	Text string `json:"text"`
}

func (a AnankeServer) ServeHTTP(w http.ResponseWriter, r *http.Request){
	path := strings.SplitN(r.RequestURI, "/", 3)

	switch r.Method {
	case "GET":
		val, _ := json.Marshal(path)
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprintf(w, string(val))
	case "POST":
		bs, _ := ioutil.ReadAll(r.Body)
		json.Unmarshal(bs, &je)
	default:
		http.Error(w, "Http method not allowed", http.StatusMethodNotAllowed)
	}
}

var je JournalEntry = JournalEntry{
	Book: 15,
	Page: 20,
	Title: "Hello world!",
	Text: "This is where I would write something!",
}

func main() {
	mux := http.NewServeMux()

	var a AnankeServer
	mux.Handle("/test/", a)
	http.ListenAndServe("0.0.0.0:6743", mux)
}
